﻿using System;
using System.CodeDom;
using System.Collections.Generic;
using OrganizationName.Common;

namespace OrganizationName.CodeGeneration
{
    public abstract class CodeProvider : UniqueIdProvider, ICodeProvider, IUniqueIdProvider, ITree<CodeProvider>
    {
        public virtual void GenerateCode(ICodeContext codeContext)
        {
            DeclarationPhase(codeContext);
            InitializationPhase(codeContext);
            FinalizationPhase(codeContext);
        }

        public abstract void DeclarationPhase(ICodeContext codeContext);
        public abstract void InitializationPhase(ICodeContext codeContext);
        public abstract void FinalizationPhase(ICodeContext codeContext);
        public abstract CodeExpression ToRightHandSideExpression(ICodeContext codeContext);

        public CodeProvider Root
        {
            get { return Parent != null ? Parent.Root : this; }
        }

        private CodeProvider _Parent;

        public CodeProvider Parent
        {
            get
            {
                return _Parent;
            }
            internal set
            {
                _Parent = value;
            }
        }

        private List<CodeProvider> _Children = new List<CodeProvider>();

        public System.Collections.Generic.ICollection<CodeProvider> Children
        {
            get
            {
                return _Children;
            }
        }
    }
}
